#Creates xml with values for sales data of products 
#and the quantity of sales in each quarter of a year
#by a combination of differing scales of two y-axes.
#The values required for building the xml is obtained as parameter arr_data
#It expects an array in which each element is 
#itself an array with first element as label, second element as sales value
#and third element as quantity value


xml = Builder::XmlMarkup.new

xml.map(:borderColor=>'FFFFFF', :connectorColor=>'000000', :fillAlpha=>'70', :hoverColor=>'FFFFFF', :showBevel=>'0') do 
  xml.colorRange() do 
#TODO: do some dynamics

  temp_arr = []
  for item in arr_data
    temp_arr << Integer(item[1]["sum_calls"]) #TODO: subject to change
    end
    if temp_arr != nil and temp_arr.size != 0 
    color_codes = ["FFFFCC", "FFFF99", "FFFF66", "FFFF33", "FFFF00", "FFCC99", "FF9933", "FF6666", "FF0066", "FF0033", "FF0000"]
    min = temp_arr.min
    median = (temp_arr.sort)[Integer(temp_arr.size/2)]
    step = 5 #(median-min)*2 / (color_codes.size-1)

      for i in 0...color_codes.size
    xml.color(:minValue=>Integer(step*i), :maxValue=>Integer(step*i + step), :displayValue=>Integer(step*i + step), :color=>color_codes[i])
      end 
  end

=begin
    xml.color(:minValue=>'0', :maxValue=>'80', :displayValue=>'Poor', :color=>'CC0001')
  xml.color(:minValue=>'80', :maxValue=>'90', :displayValue=>'Average', :color=>'FFCC33')
    xml.color(:minValue=>'90', :maxValue=>'100', :displayValue=>'Good', :color=>'069F06')
=end
  end
  
#?from=#{params["from"]}&iso=#{item.iso_country_code}&p=#{params["p"]}&to=#{params["to"]}
  xml.data() do
    for item in arr_data
      xml.entity(:id=>item[0], :value=>item[1]["sum_calls"], :link=>"google.com")  #<entity id="AL" value="80.3" />
    end
  end

  xml.styles() do
    xml.definition() do
      xml.style(:type=>"animation", :name=>"animX", :param=>"_xscale", :start=>"0", :duration=>"1")
    xml.style(:type=>"animation", :name=>"animY", :param=>"_yscale", :start=>"0", :duration=>"1")
    end
    xml.application() do
      #xml.apply(:toObject=>"PLOT", :styles=>"animX,animY") 
      # This line has a problem. I don't know why
    end
  end

end